package com.atguigu.wordcount.keyvaluetextinputformat;

import com.atguigu.constant.CommonConstantValue;
import com.atguigu.util.HdfsUtil;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.KeyValueLineRecordReader;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

/**
 * @ClassName WcDriver
 * @DeScription WcDriver
 * @Author 剧情再美终是戏
 * @Date 2019/11/16 14:48
 * @Version 1.0
 **/
public class WcDriver {

    public static void main(String[] args) throws Exception {

        // 输入输出位置
        Path inputPath = new Path(CommonConstantValue.HADOOP_TEST_WORD_FILE + "\\mrinput\\keyvalue");
        Path outPath = new Path(CommonConstantValue.HADOOP_TEST_WORD_FILE + "\\mroutput\\keyvalue");

        // 清除输出文件路径
        Configuration conf = new Configuration();
        FileSystem fs = HdfsUtil.getFileSystem(conf);
        HdfsUtil.delete(fs, outPath, true);

        // 初始化一个job
        conf.set(KeyValueLineRecordReader.KEY_VALUE_SEPERATOR, "*"); // 设置keyvalueTextInputFormat分割符
        Job job = Job.getInstance(conf);

        // 设置job的参数
        // 包括job的名称
        job.setJobName("wc");

        // mapper类，reducer类
        job.setMapperClass(WcMapper.class);
        job.setReducerClass(WcReducer.class);

        // 设置map,reducer KV格式
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        // 输入输出位置
        FileInputFormat.setInputPaths(job, inputPath);
        FileOutputFormat.setOutputPath(job, outPath);

        // 设置combineInputFormat相关参数
        job.setInputFormatClass(KeyValueTextInputFormat.class);

        // 提交job
        job.waitForCompletion(true);
    }
}
